<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{category.top.name}}</XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem v-if="category.sub" :key="category.sub.id">{{category.sub.name}}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>
<script>
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { watch, ref } from 'vue'
export default {
  name: 'SubFilter',
  setup () {
    const router = useRoute()
    const store = useStore()
    const category = ref({})
    watch([() => router.params.id, store.state.category], () => {
      store.state.category.list.forEach(element => {
        if (element.children) {
          element.children.forEach(item => {
            if (item.id === router.params.id) {
              category.value.sub = item
              category.value.top = element
            }
          })
        }
      })
    }, {
      immediate: true
    })
    return { category }
  }

}
</script>
<style scoped lang='less'>
</style>
